package com.jianbing.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jianbing.entity.Result;
import com.jianbing.entity.TaskStatus;
import com.jianbing.service.ThxdService;
import com.jianbing.util.StringUtils;

import net.sf.json.JSONObject;

/**
 * Created with IntelliJ IDEA. User: LFL Date: 2017/4/26 Time: 20:50
 * Description:
 */
@Controller
@RequestMapping("/thxd")
public class ThxdController {
	Logger logger = Logger.getLogger(ThxdController.class);

	@Autowired
	private ThxdService thxdService;

	/**
	 * 
	 * 获取通话详单城市配置
	 * 
	 * @author cwl
	 * @date 2017年5月11日
	 *
	 * @param reponse
	 * @param task1
	 * @return
	 */
	@RequestMapping(value = "/getThxdCitySettings", method = RequestMethod.GET)
	@ResponseBody
	public Result getThxdCitySettings(HttpServletRequest request, HttpServletResponse response, String appKey,
			String token, String provName, String operatorTypeName) {
		Result result = new Result();
		if (StringUtils.isEmpty(appKey) || StringUtils.isEmpty(token) || StringUtils.isEmpty(provName)) {
			result.setCode(-1);
			result.setMsg("参数错误");
			return result;
		}
		return thxdService.getThxdSettings(result, appKey, token, provName, operatorTypeName);

	}

	/**
	 * 
	 * 合作方获取通话详单数据
	 * 
	 * @author cwl
	 * @date 2017年5月13日
	 *
	 * @param request
	 * @param response
	 * @param body
	 * @return
	 */
	@RequestMapping(value = "/queryThxdData", method = RequestMethod.POST)
	@ResponseBody
	public Result queryThxdData(HttpServletRequest request, HttpServletResponse response, @RequestBody String body) {
		Result result = new Result();
		logger.info("查询通话详单数据" + body);
		try {
			JSONObject json = JSONObject.fromObject(body);
			if (json.get("sign") == null || json.get("params") == null) {
				result.setCode(101);
				result.setMsg("参数错误");
				return result;
			}
			return thxdService.queryThxdData(result, json);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
			return result;
		}
	}

	/**
	 * 
	 * 更新通话详单任务状态（如果沒有对应的数据，新增一条）
	 * 
	 * @author cwl
	 * @date 2017年5月13日
	 *
	 * @param request
	 * @param response
	 * @param orderSn
	 * @param taskId
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/updateThxdTaskStatus", method = RequestMethod.POST)
	@ResponseBody
	public Result updateThxdTaskStatus(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute TaskStatus taskStatus) {
		Result result = new Result();
		if (StringUtils.isEmpty(taskStatus.getOrderSn()) || StringUtils.isEmpty(taskStatus.getStatus())
				|| taskStatus.getTaskId() == null) {
			result.setCode(-1);
			result.setMsg("参数错误");
			return result;
		}
		try {
			thxdService.updateThxdTaskStatus(taskStatus);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
			return result;
		}
		return result;
	}

	/**
	 * 
	 * 查询通话详单任务状态
	 * 
	 * @author cwl
	 * @date 2017年5月13日
	 *
	 * @param request
	 * @param response
	 * @param orderSn
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/queryThxdTaskStatus", method = RequestMethod.GET)
	@ResponseBody
	public Result queryThxdTaskStatus(HttpServletRequest request, HttpServletResponse response, String orderSn,
			Integer taskId, String status) {
		Result result = new Result();
		ModelMap model = new ModelMap();
		model.addAttribute("list", thxdService.queryThxdTaskStatus(orderSn, taskId, status));
		result.setData(model);
		return result;
	}

}
